<template>
  <div class='edit-sensor-dialog-container'
       v-if="visible">
    <el-dialog :visible.sync="visible"
               @close="dialogClose"
               :title="status==='update'?'编辑传感器':'新增传感器'">
      <el-form :model="editSensorData"
               :disabled="status==='detail'"
               label-width="150px"
               class="edit-company-form"
               :rules="editSensorFormRules"
               ref="editSensorForm">
        <el-row>
          <el-col :span="11">
            <el-form-item label="传感器类型"
                          prop="sen_type">
              <el-select v-model="editSensorData.sen_type">
                <el-option v-for="(item, index) in sensorTypeList"
                           :key="index"
                           :label="item.name"
                           :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11"
                  :offset="1">
            <el-form-item label="传感器规格"
                          prop="sen_spec">
              <el-select v-model="editSensorData.sen_spec"
                         placeholder="请输入传感器规格">
                <el-option v-for="(item, index) in sensorSpecList"
                           :key="index"
                           :label="item.name"
                           :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="传感器编号"
                          prop="sen_num">
              <el-input v-model="editSensorData.sen_num"
                        placeholder="请输入传感器编号">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11"
                  :offset="1">
            <el-form-item label="横截面积(㎡)"
                          prop="sen_area">
              <el-input v-model="editSensorData.sen_area"
                        placeholder="请输入横截面积">
              </el-input>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="最小工作环境温度"
                          prop="temp_min">

              <el-input v-model="editSensorData.temp_min"
                        placeholder="请输入最小工作环境温度">
              </el-input>

            </el-form-item>
          </el-col>
          <el-col :span="11"
                  :offset="1">
            <el-form-item label="最大工作环境温度"
                          prop="temp_max">

              <el-input v-model="editSensorData.temp_max"
                        placeholder="请输入最大工作环境温度">
              </el-input>

            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="标定系数(K)"
                          prop="sen_coe">
              <el-input v-model="editSensorData.sen_coe"
                        placeholder="请输入标定系数(K)">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11"
                  :offset="1">
            <el-form-item label="计算修正值(B)"
                          prop="sen_cor">
              <el-input v-model="editSensorData.sen_cor"
                        placeholder="请输入计算修正值(B)">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="初始读数(FO)"
                          prop="sen_ini">
              <el-input placeholder="请输入初始读数(FO)"
                        v-model="editSensorData.sen_ini">

              </el-input>

            </el-form-item>
          </el-col>
          <el-col :span="11"
                  :offset="1">
            <el-form-item label="线圈电阻"
                          prop="sen_res">
              <el-input placeholder="请输入线圈电阻"
                        v-model="editSensorData.sen_res">

              </el-input>

            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="压力报警下限"
                          prop="sen_low">
              <el-input placeholder="请输入压力报警下限"
                        v-model="editSensorData.sen_low">

              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11"
                  :offset="1">
            <el-form-item label="压力报警上限"
                          prop="sen_upp">
              <el-input placeholder="请输入压力报警上限"
                        v-model="editSensorData.sen_upp">

              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="弹性模量(E)"
                          prop="sen_mod">
              <el-input v-model="editSensorData.sen_mod"
                        placeholder="请输入弹性模量(E)">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11"
                  :offset="1">
            <el-form-item label="防渗水压力"
                          prop="sen_sep">
              <el-input placeholder="请输入防渗水压力"
                        v-model="editSensorData.sen_sep"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="电缆长度"
                          prop="sen_cab">
              <el-input v-model="editSensorData.sen_cab"
                        placeholder="请输入电缆长度">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11"
                  :offset="1">
            <el-form-item label="绝缘电阻最小值"
                          prop="sen_ins">
              <el-input v-model="editSensorData.sen_ins"
                        placeholder="请输入绝缘电阻最小值">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="初始温度值(T0)"
                          prop="start_temp">
              <el-input placeholder="请输入初始温度值(T0)"
                        v-model="editSensorData.start_temp">

              </el-input>
            </el-form-item>
          </el-col>

        </el-row>

      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogClose">取 消</el-button>
        <el-button v-if="status!=='detail'"
                   type="primary"
                   @click="editSensor">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { SensorListApi } from '@/services/EquipmentManage/'
export default {
  data () {
    return {
      editSensorData: {},
      visible: false,
      status: 'create',
      editSensorFormRules: {
        sen_type: [
          { required: true, message: '请选择传感器类型', trigger: 'change' }
        ],
        sen_spec: [
          { required: true, message: '请选择传感器规格', trigger: 'change' }
        ],
        sen_ini: [
          { required: true, message: '请输入初始读数', trigger: 'change' }
        ],
        sen_num: [
          { required: true, message: '请输入传感器编号', trigger: 'change' }
        ],
        temp_min: [
          { required: true, message: '请输入工作最低温度', trigger: 'change' }
        ],
        temp_max: [
          { required: true, message: '请输入工作最高温度', trigger: 'change' }
        ],
        sen_coe: [
          { required: true, message: '请输入标定系数(K)', trigger: 'change' }
        ],
        sen_cor: [
          { required: true, message: '请输入计算修正值(B)', trigger: 'change' }
        ],
        sen_mod: [
          { required: true, message: '请输入弹性模量', trigger: 'change' }
        ],
        sen_sep: [
          { required: true, message: '请输入防渗水压力', trigger: 'change' }
        ],
        sen_res: [
          { required: true, message: '请输入线圈电阻', trigger: 'change' }
        ],
        sen_low: [
          { required: true, message: '请输入压力报警下限', trigger: 'change' }
        ],
        sen_upp: [
          { required: true, message: '请输入压力报警上限', trigger: 'change' }
        ],
        sen_cab: [
          { required: true, message: '请输入电缆长度', trigger: 'change' }
        ],
        sen_ins: [
          { required: true, message: '请输入绝缘电阻最小值', trigger: 'change' }
        ],
        start_temp: [
          { required: true, message: '请输入初始温度值', trigger: 'change' }
        ],
        sen_area: [
          { required: true, message: '请输入横截面积', trigger: 'change' }
        ]
      },
      // 传感器类型标准码列表
      sensorTypeList: [],
      // 传感器规格标准码列表
      sensorSpecList: []
    }
  },
  methods: {
    handleCompanylogoSuccess () {

    },
    beforeCompanylogoUpload () {

    },
    editSensor () {
      this.$refs.editSensorForm.validate((valid) => {
        if (valid) {
          if (this.dialogStatus === 'create') {
            this.createSensor()
          } else if (this.status === 'update') {
            this.updateSensor()
          }
        }
      })
    },
    // 新增传感器
    createSensor () {
      SensorListApi.createSensor({
        data: this.editSensorData
      }).then(res => {
        // console.log(res, 'create sensor')
        if (res.code === 0) {
          this.$message.success('新增传感器成功')
          this.$emit('callback', 'success')
          this.dialogClose()
        }
      }, err => {
        console.log(err)
      })
    },
    // 修改传感器
    updateSensor () {
      this.editSensorData.id = this.editSensorData.sensor_id
      SensorListApi.updateSensor({
        data: this.editSensorData
      }).then(res => {
        // console.log(res, 'update sensor')
        if (res.code === 0) {
          this.$message.success('修改传感器成功')
          this.$emit('callback', 'success')
          this.dialogClose()
        }
      }, err => {
        console.log(err)
      })
    },
    dialogClose () {
      this.$emit('update:dialogVisible', false)
    }
  },
  props: {
    dialogVisible: Boolean,
    dialogStatus: String,
    dialogForm: Object,
    callback: Function
  },
  watch: {
    dialogVisible (newval, oldval) {
      this.visible = newval
    },
    dialogForm: {
      handler (newval, oldval) {
        this.editSensorData = JSON.parse(JSON.stringify(newval))
        if (this.editSensorData.sen_type) {
          this.editSensorData.sen_type += ''
        }

        // console.log(this.editSensorData)
      },
      deep: true
    },
    dialogStatus (newval, oldval) {
      this.status = newval
      //console.log(newval, '123')
    }
  },
  created () {
    // this.editSensorData = this.dialogForm
    // this.visible = this.dialogVisible
    this.sensorTypeList = JSON.parse(window.localStorage.getItem('sensorTypeList'))
    this.sensorSpecList = JSON.parse(window.localStorage.getItem('sensorSpecList'))
  },
  mounted () {
  }
}
</script>
<style scoped lang='scss'>
.edit-sensor-dialog-container {
  .el-select {
    width: 100%;
  }
}
</style>
